//
//  XRBaseTableViewController.m
//  LiveIMTool
//
//  Created by NeiQuan on 16/10/26.
//  Copyright © 2016年 AlexiChen. All rights reserved.
//

#import "XRBaseTableViewController.h"
#import "XRCommonTableViewCell.h"
#import "MJrefresh.h"
#import "MJRefreshGifHeader.h"
#import "UIScrollView+MJRefresh.h"
#import "UIScrollView+MJExtension.h"
#import "UIView+MJExtension.h"

#import "MJRefreshNormalHeader.h"
#import "MJRefreshGifHeader.h"

#import "MJRefreshBackNormalFooter.h"
#import "MJRefreshBackGifFooter.h"
#import "MJRefreshAutoNormalFooter.h"
#import "MJRefreshAutoGifFooter.h"
#import "UITableViewPlaceHolderHeader.h"
#import "TableViewNoNetWorkView.h"//无网络
#import "TableViewNoDataView.h"//无数据
#import "AFNetworking.h"
#import "YeeRefreshGifHeader.h"
@interface XRBaseTableViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL          _isNetWorkConnect;
    NSInteger      _NetWorkStatus;
}
@end

@implementation XRBaseTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configNetSevices];
}
#pragma mark 添加网络监听
-(void)configNetSevices
{
    [self congigNetWorkReachable];
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self  addtableView];
}
#pragma mark --tableView
-(void)addtableView
{
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 0) style:UITableViewStyleGrouped];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    _tableView.tableFooterView=[[UIView alloc] init];
    [_tableView setFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight-kNavStausHeight-kNavBarHeight)];
    if (_hasTabbar){
        [_tableView setFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight-49-kNavStausHeight-kNavBarHeight)];
    }
    [_tableView setBackgroundColor:kAppBakgroundColor];
    [self.view addSubview:_tableView];
}
#pragma mark --添加刷新事件
#pragma mark --头部刷新
-(void)tablePullupHeaderRefresh:(void (^)(void))headerBlock
{
    __weak typeof(self)weakself=self;
    MJRefreshStateHeader  *headerView=[MJRefreshStateHeader  headerWithRefreshingBlock:^{
        if (headerBlock)
        {
            headerBlock();
        }
    }];
    weakself.tableView.mj_header=headerView;
}
#pragma mark --尾部刷新
-(void)tablePullDownFootRefresh:(void (^)(void))footerBlock
{
    __weak typeof(self)weakself=self;
    MJRefreshBackNormalFooter  *footrefreshView=[MJRefreshBackNormalFooter  footerWithRefreshingBlock:^{
        if (footerBlock)
        {
           footerBlock();
        }
    }];
    weakself.tableView.mj_footer=footrefreshView;
}
#pragma mark --停止刷新
-(void)tableViewEndRefresh
{
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    [self.tableView plc_reloadData];
}
- (UIView *)makePlaceHolderView
{
    UIView *taobaoStyle = [self taoBaoStylePlaceHolder];
    return taobaoStyle;
}
-(UIView*)taoBaoStylePlaceHolder
{
    if (_NetWorkStatus==AFNetworkReachabilityStatusNotReachable)//网络可连接
    {
        
        TableViewNoNetWorkView  *noNetWorkView=[[TableViewNoNetWorkView alloc] initWithFrame:self.tableView.bounds reloadBlock:^{
            
            
        }];
        return noNetWorkView;
    }else
    {
        UIView *headerView=self.tableView.tableHeaderView ;
        NSLog(@"%f",headerView.frame.size.height);
        
      TableViewNoDataView *noDataPlaceHolder = [[TableViewNoDataView alloc] initWithFrame:CGRectMake(0, headerView.frame.size.height, kMainScreenWidth, self.tableView.frame.size.height- headerView.frame.size.height)];
        return noDataPlaceHolder;
 
    }
}
- (BOOL)enableScrollWhenPlaceHolderViewShowing
{
    return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([ UITableViewCell class])];
    }
    cell.textLabel.text=@"CoderYee";
    return cell;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] init];
    return view;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 0.01)];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 46;
}
#pragma mark --点击单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    
}
#pragma mark - 网络状态的实时检测；
- (void)congigNetWorkReachable
{
    //开启网络监视器；
    AFNetworkReachabilityManager *afNetworkReachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [afNetworkReachabilityManager startMonitoring];
    [afNetworkReachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
     {
        
        switch (status)
          {
            case AFNetworkReachabilityStatusNotReachable:
              {
               
                  _NetWorkStatus=AFNetworkReachabilityStatusNotReachable;
                //PPRSLog(@"网络不通：%f", rect.origin.y );
                break;
             }
            case AFNetworkReachabilityStatusReachableViaWiFi:
              {
                  _NetWorkStatus=AFNetworkReachabilityStatusReachableViaWiFi;

                //PPRSLog(@"网络通过WIFI连接：%f", rect.origin.y );
                break;
             }
            case AFNetworkReachabilityStatusReachableViaWWAN:
              {
                  _NetWorkStatus=AFNetworkReachabilityStatusReachableViaWWAN;

               // PPRSLog(@"网络通过无线连接：%f", rect.origin.y );
                break;
             }
            default:
                break;
        }
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
